package com.xxtx.trade.common.util;

import com.xxtx.trade.common.util.cache.CacheManager;
import com.xxtx.trade.jwt.BaseContextHandler;
import com.xxtx.trade.sysUser.enums.TradeSysUserEnum;
import com.xxtx.trade.sysUser.vo.TradeSysUserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

@Service
public class SpringUserContext {

    @Autowired
    private CacheManager cacheManager;

    /**
     * 获取当前登录用户信息(登录时保存)
     * @return
     */
    public TradeSysUserVo getCurrentLoginUser(){
        try {
            TradeSysUserVo vo = (TradeSysUserVo) cacheManager.get(TradeSysUserEnum.ACCOUNT_STATE_NORMAL.getCode() + BaseContextHandler.getUserID());
            return vo;
        }catch (Exception e){
            return null;
        }
    }

    public static HttpServletRequest getHttpServletRequest(){
        return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
    }
}
